package com.wanxi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.NewsType;
import com.wanxi.service.NewsService;
import com.wanxi.vo.NewsVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/news")
@CrossOrigin(origins = "http://localhost:8081")
public class NewsController {
    @Autowired
    NewsService newsService;

    @PutMapping("/add")
    @PreAuthorize("hasAuthority('insertNews')")
    public ResultDTO addNews(NewsVO newsVO, Long newsTypeID){
        newsVO.setNewsType(NewsType.builder().id(newsTypeID).build());
        return newsService.addNews(newsVO);
    }

    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('deleteNews')")
    public ResultDTO deleteNews(NewsVO newsVO){
        return newsService.deleteNews(newsVO);
    }

    @GetMapping("/findAll")
    public ResultDTO findAllNews(){
        return newsService.findAllNews();
    }

    @PostMapping("/findById")
    public ResultDTO findNewsById(NewsVO newsVO){
        return newsService.findNewsById(newsVO);
    }

    @GetMapping("/findBySome")
    public ResultDTO findNewsBySome(NewsVO newsVO){
        return newsService.findNewsBySome(newsVO);
    }

    @PostMapping("/updateEnable")
    @PreAuthorize("hasAuthority('updateNews')")
    public ResultDTO updateNewsEnable(NewsVO newsVO){
        return newsService.updateEnable(newsVO);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('updateNews')")
    public ResultDTO updateNews(NewsVO newsVO,Long newsTypeID){
        newsVO.setNewsType(NewsType.builder().id(newsTypeID).build());
        return newsService.updateNews(newsVO);
    }
}
